//
//  StockInquiryViewController.m
//  ShoppingExperience
//
//  Created by LiYinDi on 15-6-1.
//  Copyright (c) 2015年 RuiBen. All rights reserved.
//

#import "StockInquiryViewController.h"
#import "StockInquiryTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "GuideListTableViewCell.h"
#import "OldMeterialTableViewCell.h"

#import "EGORefreshTableHeaderView.h"
#import "SearchMemberTableViewCell.h"

//#import "CameraImageHelper.h"
#import "UpYun.h"

#import "CaptureViewController.h"
#import "PassImageDelegate.h"
#import "FilterViewController.h"
@interface StockInquiryViewController ()<EGORefreshTableHeaderDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,PassImageDelegate,FilterViewControllerDelegate>
{
    FilterViewController *filterVC;
    EGORefreshTableHeaderView *_refreshHeaderView;
    BOOL _reloading;

    NSUInteger _productPageIndex;
    NSUInteger _productTotalCount;
    NSUInteger _productTotalPage;
    
    NSMutableArray *inquiryProductNameArray;
    NSMutableArray *inquiryProductImageArray;
    NSMutableArray *inquiryAddressArray;
    NSMutableArray *inquiryDateArray;
    NSMutableArray *inquiryWeightArray;
    NSMutableArray *inquiryPriceArray;
    NSMutableArray *inquiryItemCodeArray;
    NSMutableArray *inquiryStyleNameArray;
    NSMutableArray *inquiryTypeNameArray;
    NSMutableArray *inquiryAttributeArray;
    NSMutableArray *inquiryGoldWeightArray;
    
    NSString *itemCodeStr;
    NSString *weightStr;
    NSString *cutGoldStr;
    NSString *processPriceStr;
    NSString *todayGoldPriceStr;
    
    NSInteger priceDetailCellTag;

    NSString *imageURL;
    NSMutableArray *productImageArray;
    
    NSMutableArray *upYunImageArray;
    
    NSString *policyStr;
    NSString *signatureStr;
    
    NSString *downLoadUrlStr;
    NSInteger inquiryTableTag;
    
    BOOL isLoad;
    
    
    UITextField *inputTextField;
    
    NSMutableArray *filterTypeCodeArray ;
    NSMutableArray *inputFilterCodeArray;
    NSMutableArray *inputMinResultFilterNameArray;
    NSMutableArray *inputMaxResultFilterNameArray;
    NSMutableArray *selectFilterCodeArray;
    NSMutableArray *selectFilterNameArray;
    NSMutableArray *selectCustomTimeArray ;
    NSMutableArray *customTimeFilterTypeCodeArray ;
    NSString *startTimeStr;
    NSString *endTimeStr;

}
//@property(retain,nonatomic) CameraImageHelper *CameraHelper;
- (void)reloadTableViewDataSource;
- (void)doneLoadingTableViewData;
@end

@implementation StockInquiryViewController
-(void)viewWillAppear:(BOOL)animated
{
    if (filterVC.view!=nil) {
        
        [filterVC removeFromParentViewController];
        [filterVC.view removeFromSuperview];
        
    }

     if (isLoad==NO) {
    [_shopCartBtn addTarget:self action:@selector(shopCartBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self initInquiryList];
    [self initProductDetailView];
    
    [self initOrderFilterView];
    _productPageIndex=0;
    [self loadProductNextPage:_productPageIndex];
     }
}
-(void)initOrderFilterView
{
    inputFilterCodeArray=[NSMutableArray array];
    inputMinResultFilterNameArray=[NSMutableArray array];
    inputMaxResultFilterNameArray=[NSMutableArray array];
    filterTypeCodeArray=[NSMutableArray array];
    customTimeFilterTypeCodeArray=[NSMutableArray array];
    selectFilterCodeArray=[NSMutableArray array];
    selectFilterNameArray=[NSMutableArray array];
    selectCustomTimeArray=[NSMutableArray array];
    
    startTimeStr=@"";
    endTimeStr=@"";
    
    [_orderFilterTitleLabel setTextColor:[UIColor colorWithRed:80.0f/255.0f green:80.0f/255.0f blue:80.0f/255.0f alpha:1.0f]];
    [_orderFilterImage setImage:[UIImage imageNamed:@"shaixuandingdan_.png"]];
    // [_orderFilterViewContainer bringSubviewToFront:_resetFilterBtn];
    
    _orderFilterBtn.layer.borderColor=[[UIColor colorWithRed:220.0f/255.0f green:220.0f/255.0f blue:220.0f/255.0f alpha:1] CGColor];
    _orderFilterBtn.layer.borderWidth= 1.0f;
    _orderFilterBtn.layer.cornerRadius=4;
    
    [_orderFilterBtn addTarget:self action:@selector(orderFilterBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)orderFilterBtnClick
{
    if (filterVC.view!=nil) {
        
        [filterVC removeFromParentViewController];
        [filterVC.view removeFromSuperview];
        
    }
    filterVC=[[FilterViewController alloc] init];
    filterVC.delegate=self;
    filterVC.docTypeStr=@"ProductOH";
    filterVC.selectCustomTimeArray=[NSMutableArray arrayWithArray:selectCustomTimeArray];
    filterVC.startTimeStr=startTimeStr;
    filterVC.endTimeStr=endTimeStr;
    // NSLog(@"selectFilterNameArray=22222======%@",selectFilterNameArray);
    filterVC.selectFilterNameArray=[NSMutableArray arrayWithArray:selectFilterNameArray];
    filterVC.view.frame = CGRectMake(0,
                                     0,
                                     self.view.bounds.size.width,
                                     self.view.bounds.size.height);
    
    [self addChildViewController:filterVC];
    [self.view addSubview:filterVC.view];
}
-(void)confirmFilterBtnClick:(NSMutableArray *)commonFilterTypeCodeArray commonSelectFilterCodeArray:(NSMutableArray *)commonSelectFilterCodeArray commonInputFilterCodeArray:(NSMutableArray *)commonInputFilterCodeArray commonInputMinResultFilterNameArray:(NSMutableArray *)commonInputMinResultFilterNameArray commonInputMaxResultFilterNameArray:(NSMutableArray *)commonInputMaxResultFilterNameArray commonCustomTimeFilterTypeCodeArray:(NSMutableArray *)commonCustomTimeFilterTypeCodeArray commonSelectCustomTimeArray:(NSMutableArray *)commonSelectCustomTimeArray commonSelectFilterNameArray:(NSMutableArray *)commonSelectFilterNameArray  commonStartTimeStr:(NSString *)commonStartTimeStr commonEndTimeStr:(NSString *)commonEndTimeStr isDefault:(NSString *)isDefault
{
    
    if ([isDefault isEqualToString:@"Y"]) {
        //  NSLog(@"_isDefault0000===%@",isDefault);
        [_orderFilterTitleLabel setTextColor:[UIColor colorWithRed:80.0f/255.0f green:80.0f/255.0f blue:80.0f/255.0f alpha:1.0f]];
        [_orderFilterImage setImage:[UIImage imageNamed:@"shaixuandingdan_.png"]];
    }
    else{
        // NSLog(@"_isDefault1111===%@",isDefault);
        [_orderFilterTitleLabel setTextColor:[UIColor colorWithRed:203.0f/255.0f green:164.0f/255.0f blue:67.0f/255.0f alpha:1.0f]];
        [_orderFilterImage setImage:[UIImage imageNamed:@"yishaixuan_.png"]];
    }
    
    filterTypeCodeArray=commonFilterTypeCodeArray;
    selectFilterCodeArray=commonSelectFilterCodeArray;
    inputFilterCodeArray=commonInputFilterCodeArray;
    inputMinResultFilterNameArray=commonInputMinResultFilterNameArray;
    inputMaxResultFilterNameArray=commonInputMaxResultFilterNameArray;
    customTimeFilterTypeCodeArray=commonCustomTimeFilterTypeCodeArray;
    selectCustomTimeArray=commonSelectCustomTimeArray;
    selectFilterNameArray=commonSelectFilterNameArray;
    
    startTimeStr=commonStartTimeStr;
    endTimeStr=commonEndTimeStr;
    
    
    [inquiryProductImageArray removeAllObjects];
    [inquiryProductNameArray removeAllObjects];
    [inquiryWeightArray removeAllObjects];
    [inquiryDateArray removeAllObjects];
    [inquiryPriceArray removeAllObjects];
    [inquiryItemCodeArray removeAllObjects];
    [inquiryAddressArray removeAllObjects];
    [inquiryAttributeArray removeAllObjects];
    [inquiryGoldWeightArray removeAllObjects];
    [inquiryStyleNameArray removeAllObjects];
    [inquiryTypeNameArray removeAllObjects];
    
    
    _productPageIndex=0;
    [self loadProductNextPage:_productPageIndex];
    
}
-(void)shopCartBtnClick
{
    isLoad=NO;
    if ([self.delegate respondsToSelector:@selector(stockInquiryShopCartClick)]) {
        
        [self.delegate stockInquiryShopCartClick];
    }
    
}

-(void)initInquiryList
{
    inquiryTableTag=0;
     downLoadUrlStr=@"";
    _searchProductTextField.text=@"";
   inquiryProductNameArray=[NSMutableArray array];
   inquiryProductImageArray=[NSMutableArray array];
    inquiryAddressArray=[NSMutableArray array];
    inquiryAttributeArray=[NSMutableArray array];
    inquiryGoldWeightArray=[NSMutableArray array];
   inquiryDateArray=[NSMutableArray array];
    inquiryWeightArray=[NSMutableArray array];
   inquiryPriceArray=[NSMutableArray array];
    inquiryItemCodeArray=[NSMutableArray array];
    inquiryStyleNameArray=[NSMutableArray array];
   inquiryTypeNameArray=[NSMutableArray array];
    
  
}
-(void)initProductDetailView
{
    policyStr=@"";
    signatureStr=@"";
    imageURL=@"";
    _productImageBtn.hidden=YES;
    _upImageButton.hidden=NO;
    CGRect frame=_rightViewContainer.frame;
    frame.origin.y=_upImageButton.frame.origin.y+_upImageButton.frame.size.height+10;
    _rightViewContainer.frame=frame;
    
    CGRect viewFrame1=_photoPickerViewContainer.frame;
    viewFrame1.origin.y=768;
    _photoPickerViewContainer.frame=viewFrame1;
    
    upYunImageArray=[NSMutableArray array];
    
    _upImageButton.layer.borderColor=[[UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1] CGColor];
    _upImageButton.layer.borderWidth= 1.0f;
    
    [_productImageBtn addTarget:self action:@selector(upImageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_upImageButton addTarget:self action:@selector(upImageBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    _addToShopCartBtn.layer.borderColor=[[UIColor colorWithRed:210.0f/255.0f green:171.0f/255.0f blue:57.0f/255.0f alpha:1] CGColor];
    _addToShopCartBtn.layer.borderWidth= 1.0f;
    _addToShopCartBtn.layer.cornerRadius=3;

    for (UIView *v in _rightScrollView.subviews) {
        if (v.tag==9) {
            
            [v removeFromSuperview];
        }
        
    }
    CGRect frame1=_productDetailViewContainer.frame;
    frame1.origin.y=768;
    [_productDetailViewContainer setFrame:frame1];
     [_productDetailAlphaView setBackgroundColor:[Global parseColor:WESTORE_TRANSCLUCENT]];
    [_productDetailAlphaView addTarget:self action:@selector(productDetailAlphaViewClick) forControlEvents:UIControlEventTouchUpInside];
}
-(void)upImageBtnClick:(id)sender
{
    
    [UIView animateWithDuration:0.5f animations:^
     {
         CGRect viewFrame=_photoPickerViewContainer.frame;
         viewFrame.origin.y=0;
         _photoPickerViewContainer.frame=viewFrame;
     }completion:^(BOOL finished) {
         
         
     }];
//    _CameraHelper = [[CameraImageHelper alloc]init];
//    [_CameraHelper startRunning];
//    [_CameraHelper embedPreviewInView:_cameraImageView];
//    
//    [_CameraHelper changePreviewOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
    //
}
#pragma 拍照选择照片协议方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    isLoad=YES;
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    
    NSData *data;
    
    if ([mediaType isEqualToString:@"public.image"]){
        
        //切忌不可直接使用originImage，因为这是没有经过格式化的图片数据，可能会导致选择的图片颠倒或是失真等现象的发生，从UIImagePickerControllerOriginalImage中的Origin可以看出，很原始，哈哈
        UIImage *originImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        // NSLog(@"originImage.",originImage.size);
        //图片压缩，因为原图都是很大的，不必要传原图
        UIImage *scaleImage = [self scaleImage:originImage toScale:0.7];
        
        //以下这两步都是比较耗时的操作，最好开一个HUD提示用户，这样体验会好些，不至于阻塞界面
        if (UIImagePNGRepresentation(scaleImage) == nil) {
            //将图片转换为JPG格式的二进制数据
            data = UIImageJPEGRepresentation(scaleImage, 1);
        } else {
            //将图片转换为PNG格式的二进制数据
            data = UIImagePNGRepresentation(scaleImage);
        }
        
        //将二进制数据生成UIImage
        UIImage *image = [UIImage imageWithData:data];
        
        //将图片传递给截取界面进行截取并设置回调方法（协议）
        CaptureViewController *captureView = [[CaptureViewController alloc] init];
        captureView.delegate = self;
        captureView.image = image;
        //隐藏UIImagePickerController本身的导航栏
        picker.navigationBar.hidden = NO;
        [picker pushViewController:captureView animated:YES];
        
    }
}

#pragma mark - 图片回传协议方法
-(void)passImage:(UIImage *)image
{
    _productImageBtn.hidden=NO;
    _upImageButton.hidden=YES;
    _productImage.image = image;
    
    // isLoad=NO;
    
    CGRect frame=_rightViewContainer.frame;
    frame.origin.y=_productImageBtn.frame.origin.y+_productImageBtn.frame.size.height+10;
    _rightViewContainer.frame=frame;
    
    [upYunImageArray removeAllObjects];
    if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        [params setObject:@"Product" forKey:@"uploadType"];
        // NSLog(@"params====%@",params);
        [HttpRequest requestLoginData:@"System/GetFormParams" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            // NSLog(@"serrr111==%@",severDataDict.dataDict);
            // [_cashierSearchMemberTextField resignFirstResponder];
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    [Global setUpYunUrl:[severDataDict.dataDict objectForKey:@"action"]];
                    signatureStr=[severDataDict.dataDict objectForKey:@"signature"];
                    policyStr=[severDataDict.dataDict objectForKey:@"policy"];
                    
                    [Global setSignatureStr:signatureStr];
                    [Global setPolicyStr:policyStr];
                    
                    downLoadUrlStr=[severDataDict.dataDict objectForKey:@"ImageZone"];
                    
                    [self upToUpYun];
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    
                    if(imageURL.length>0)
                    {
                        [_productImage setImageWithURL:[NSURL URLWithString:imageURL]];
                        
                        _productImageBtn.hidden=NO;
                        
                        _upImageButton.hidden=YES;
                        
                        CGRect frame=_rightViewContainer.frame;
                        frame.origin.y=_productImageBtn.frame.origin.y+_productImageBtn.frame.size.height+10;
                        _rightViewContainer.frame=frame;
                        
                    }
                    else
                    {
                        _productImageBtn.hidden=YES;
                        _productImage.image =nil;
                        _upImageButton.hidden=NO;
                        
                        CGRect frame=_rightViewContainer.frame;
                        frame.origin.y=_upImageButton.frame.origin.y+_upImageButton.frame.size.height+10;
                        _rightViewContainer.frame=frame;
                        
                        
                    }
                    
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    [Global loginViewController:self noteStr:requestResultStr];
                     //   [Global loginViewController:self];
                    }

                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
    }
    
}

#pragma mark- 缩放图片
-(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize));
    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height *scaleSize)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

-(IBAction)albumOrCameraBtnClick:(UIButton *)sender
{
    CGRect viewFrame=_photoPickerViewContainer.frame;
    viewFrame.origin.y=768;
    _photoPickerViewContainer.frame=viewFrame;
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    switch (sender.tag) {
        case 20000://Take picture
            
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                
            }else{
                NSLog(@"模拟器无法打开相机");
            }
            [self presentViewController:picker animated:YES completion:nil];
            break;
            
        case 20001://From album
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:picker animated:YES completion:nil];
            break;
            
        default:
            
            break;
    }
    
}

-(IBAction)cancelBtnClick:(id)sender
{
    // _CameraHelper.selectOrCancel=@"CANCEL";
    CGRect viewFrame=_photoPickerViewContainer.frame;
    viewFrame.origin.y=768;
    _photoPickerViewContainer.frame=viewFrame;
    // [_CameraHelper stopRunning];
    
    
}
-(void)upToUpYun
{
     isLoad=NO;
    __block NSInteger count=0;
    UpYun *uy = [[UpYun alloc] init];
    uy.successBlocker = ^(id data)
    {
        // NSLog(@"zuzhijigouDATA======%@",data );
        count++;
        // NSLog(@"count======%d",count );
        // NSLog(@"_pictureView.subviews.count======%d",_pictureView.subviews.count );
        [Global hideProgressHUDInternal:self.view];
        NSString *imageStr;
        NSString *imageWIDTH;
        NSString *imageHEIGHT;
        imageStr=[data objectForKey:@"url"];
        NSString *imgUrl=[NSString stringWithFormat:@"/%@",imageStr];
        imageWIDTH=[data objectForKey:@"image-width"];
        imageHEIGHT=[data objectForKey:@"image-height"];
        NSMutableDictionary *upYunImageDict=[NSMutableDictionary dictionary];
        [upYunImageDict setObject:imgUrl forKey:@"ImageUrl"];
        [upYunImageDict setObject:imageWIDTH forKey:@"ImageWidth"];
        [upYunImageDict setObject:imageHEIGHT forKey:@"ImageHeigh"];
        [upYunImageArray addObject:upYunImageDict];
        if ([Global getLoginToken].length!=0&& upYunImageDict.count>0) {
            [Global showIndeterminateHudSub:@"上传中，请稍后。。。" delay:30 view:self.view];
            
            NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
            [params setObject:[Global getLoginToken] forKey:@"Token"];
            
            [params setObject:itemCodeStr forKey:@"ItemCode"];
            
            [params setObject:upYunImageArray forKey:@"DataLine"];
            //            NSLog(@"params===%@",params);
            [HttpRequest requestData:@"Product/UpdateItemImg" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                // NSLog(@"uploadSever===%@",severDataDict.dataDict);
                // NSLog(@"uploadSeverRESULTSTR===%@",severDataDict.resultStr);
                if (isSuccess) {
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        
                        [Global showTextHudSub:requestResultStr view:self.view];
                        // NSLog(@"touxiang====%@",severDataDict.dataDict);
                        //  [_uploadBtn setTitle:@"已上传" forState:UIControlStateNormal];
                        // [self uploadImage];
                        _productImageBtn.hidden=NO;
                        
                        _upImageButton.hidden=YES;
                        
                        
                        NSIndexPath *indexPath1= [NSIndexPath indexPathForRow:inquiryTableTag inSection:0];
                        UITableViewCell *cell = [_inquiryListTableView cellForRowAtIndexPath:indexPath1];
                        StockInquiryTableViewCell *leftCell=( StockInquiryTableViewCell *)cell;
                        
                        NSString *imgStr=[NSString stringWithFormat:@"%@%@",downLoadUrlStr,imgUrl];
                        // NSLog(@"imgStr=====%@",imgStr);
                        
                        [ leftCell.productImageView setImageWithURL:[NSURL URLWithString:imgStr]];
                        [inquiryProductImageArray replaceObjectAtIndex:inquiryTableTag withObject:imgStr];
                        
                        CGRect frame=_rightViewContainer.frame;
                        frame.origin.y=_productImageBtn.frame.origin.y+_productImageBtn.frame.size.height+10;
                        _rightViewContainer.frame=frame;
                        
                        
                        
                    }
                    else
                    {
                        
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        // [_uploadBtn setTitle:@"上传失败" forState:UIControlStateNormal];
                        // [Global showTextHud:requestResultStr view:self.view];
                        [Global showTextHudSub:requestResultStr view:self.view];
                        if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                         [Global loginViewController:self noteStr:requestResultStr];
                        //    [Global loginViewController:self];
                        }
                    }
                    
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    //  [_uploadBtn setTitle:@"上传失败" forState:UIControlStateNormal];
                    
                    [Global showTextHudSub:@"连接失败" view:self.view];
                }
            }];
        }
        
        
        
        
    };
    uy.failBlocker = ^(NSError * error)
    {
        [Global hideProgressHUDInternal:self.view];
        
        
        if(imageURL.length>0)
        {
            [_productImage setImageWithURL:[NSURL URLWithString:imageURL]];
            
            _productImageBtn.hidden=NO;
            
            _upImageButton.hidden=YES;
            
            CGRect frame=_rightViewContainer.frame;
            frame.origin.y=_productImageBtn.frame.origin.y+_productImageBtn.frame.size.height+10;
            _rightViewContainer.frame=frame;
            
        }
        else
        {
            _productImageBtn.hidden=YES;
            _productImage.image =nil;
            _upImageButton.hidden=NO;
            
            CGRect frame=_rightViewContainer.frame;
            frame.origin.y=_upImageButton.frame.origin.y+_upImageButton.frame.size.height+10;
            _rightViewContainer.frame=frame;
            
            
        }
        
        
        
        
        NSString *message = [error.userInfo objectForKey:@"message"];
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"error" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        //  NSLog(@"%@",error);
    };
    uy.progressBlocker = ^(CGFloat percent, long long requestDidSendBytes)
    {
        // [_pv setProgress:0.3333333333+percent/3];
    };
    // NSLog(@"selectePictureArray===%@",selectePictureArray);
    //for (int i=0; i<selectePictureArray.count; i++) {
    // UIImageView *imageview=(UIImageView *)[selectePictureArray  objectAtIndex:i];
    
    if (_productImage.image!=nil) {
        UIImage *img=[self imageCompress:_productImage.image];
        
        _productImageBtn.hidden=NO;
        
        _upImageButton.hidden=YES;
        
        
        CGRect frame=_rightViewContainer.frame;
        frame.origin.y=_productImageBtn.frame.origin.y+_productImageBtn.frame.size.height+10;
        _rightViewContainer.frame=frame;
        
        
        [Global showIndeterminateHudSub:@"正上传UPYUN，请稍后。。。" delay:120 view:self.view];
        uy.bucket=@"";
        [uy uploadFile:img saveKey:[self getSaveKey]];
        
    }
    //    }
    
    
}

-(NSString * )getSaveKey {
    NSDate *d = [NSDate date];
    int value = arc4random() % 1000;
    return [NSString stringWithFormat:@"/%d/%d/%d.jpg",[self getYear:d],[self getMonth:d],value];
}

- (int)getYear:(NSDate *) date{
    NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
    [formatter setTimeStyle:NSDateFormatterMediumStyle];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags = NSYearCalendarUnit;
    NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
    int year=[comps year];
    return year;
}

- (int)getMonth:(NSDate *) date{
    NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
    [formatter setTimeStyle:NSDateFormatterMediumStyle];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags = NSMonthCalendarUnit;
    NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
    int month = [comps month];
    return month;
}
-(UIImage *)imageCompress:(UIImage *)image
{
    UIImage *comImage=image;
    NSData * sendImageData = UIImageJPEGRepresentation(comImage, 1.0);
    NSUInteger sizeOrigin = [sendImageData length];
    NSUInteger sizeOriginKB = sizeOrigin / 1024;
    
    if (sizeOriginKB >200) {
        float a = 200.0000;
        float b = (float)sizeOriginKB;
        float q = sqrtf(a / b);
        
        CGSize sizeImage = [comImage size];
        CGFloat widthSmall = sizeImage.width * q;
        CGFloat heighSmall = sizeImage.height * q;
        CGSize sizeImageSmall = CGSizeMake(widthSmall, heighSmall);
        
        UIGraphicsBeginImageContext(sizeImageSmall);
        CGRect smallImageRect = CGRectMake(0, 0, sizeImageSmall.width, sizeImageSmall.height);
        [image drawInRect:smallImageRect];
        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        NSData *dataImage = UIImageJPEGRepresentation(smallImage, 1.0);
        sendImageData = dataImage;
        
        comImage= smallImage;
        // [_imageview1.image retain];
    }
    return comImage;
}

-(void)productDetailAlphaViewClick
{
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame=_productDetailViewContainer.frame;
        
        frame.origin.y=768;
        [_productDetailViewContainer setFrame:frame];
        
    } completion:^(BOOL finished){
        
    }];

}
-(void) loadProductNextPage:(NSUInteger)oldPageNum {
    if(oldPageNum!=0 && _productTotalCount<=inquiryProductImageArray.count)
        return;
    if(oldPageNum == 0){
        [inquiryProductImageArray removeAllObjects];
        [_inquiryListTableView reloadData];
    }
    _productPageIndex=oldPageNum+1;
    [self requestStockInquiryList: _productPageIndex pageSize:6];
}
-(void)requestStockInquiryList:(NSUInteger)pageIndex pageSize:(NSUInteger)pageSize
{
          // NSLog(@"rrtrtrtrtrt");
        if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        NSNumber *pageSizeNumber = [NSNumber numberWithInteger:pageSize];
        NSNumber *pageIndexNumber = [NSNumber numberWithInteger:pageIndex];
        //        NSString *pageSizeStr=[NSString stringWithFormat:@"%d",pageSize];
        //        NSString *pageIndexStr=[NSString stringWithFormat:@"%d",pageIndex];
        
        [params setObject:pageSizeNumber forKey:@"PageSize"];
        [params setObject:pageIndexNumber forKey:@"PageIndex"];
        [params setObject:_searchProductTextField.text forKey:@"KeyWord"];
        NSMutableArray *mDocumentLine=[NSMutableArray array];
        for (int i=0; i<filterTypeCodeArray.count; i++) {
            
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            //    NSLog(@"codeContentssss==%@",codeContent);
            [dic setValue:[filterTypeCodeArray objectAtIndex:i] forKey:@"FilterTypeCode"];
            //  NSLog(@"countContentssss==%@",countContent);
            [dic setValue:[selectFilterCodeArray objectAtIndex:i] forKey:@"FilterCode"];
            [mDocumentLine addObject:dic];
            
        }
        
        NSMutableArray *inputDocumentLine=[NSMutableArray array];
            for (int i=0; i<inputFilterCodeArray.count; i++) {
                
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                
                [dic setValue:[inputFilterCodeArray objectAtIndex:i] forKey:@"FilterTypeCode"];
                
                [dic setValue:[inputMinResultFilterNameArray objectAtIndex:i] forKey:@"Value1"];
                [dic setValue:[inputMaxResultFilterNameArray objectAtIndex:i] forKey:@"Value2"];
                [inputDocumentLine addObject:dic];
                
            }
           // NSMutableArray *mDocumentLine1=[NSMutableArray array];
            for (int i=0; i<customTimeFilterTypeCodeArray.count; i++) {
                
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                //    NSLog(@"codeContentssss==%@",codeContent);
                [dic setValue:[customTimeFilterTypeCodeArray objectAtIndex:i] forKey:@"FilterTypeCode"];
                //  NSLog(@"countContentssss==%@",countContent);
                [dic setValue:startTimeStr forKey:@"Value1"];
                [dic setValue:endTimeStr forKey:@"Value2"];
                [inputDocumentLine addObject:dic];
                
            }

        [params setObject:mDocumentLine forKey:@"DataLineFileterParam"];
        [params setObject:inputDocumentLine forKey:@"DataLineFileterParam2"];

      //  NSLog(@"paramsLIST====%@",params);
        [HttpRequest requestData:@"StoreManager/StoreWhsItemList" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    
                    float gramInteger=[[severDataDict.dataDict objectForKey:@"DocGramTotal"] floatValue];
                    float priceInteger=[[severDataDict.dataDict objectForKey:@"DocPriceTotal"] floatValue];
                    _productTotalCount=[[severDataDict.dataDict objectForKey:@"TotalCount"] integerValue];
                    _inquiryTotalCount.text=[NSString stringWithFormat:@"本店库存有%lu件产品",(unsigned long)_productTotalCount];
                    _inquiryTotalCount.text=[NSString stringWithFormat:@"本店库存有%lu件产品  共%@g   ¥%@",(unsigned long)_productTotalCount,[Global floatConvertPercentile:gramInteger],[Global floatConvertPercentile:priceInteger]];

                    
                    if (_productTotalCount%6!=0) {
                        _productTotalPage=_productTotalCount/6+1;
                    }
                    else if (_productTotalCount%6==0)
                    {
                        _productTotalPage=_productTotalCount/6;
                    }
                   
                  NSMutableArray  *inquiryListArray=[severDataDict.dataDict objectForKey:@"DataLine"];
                 //   int total=0;
                    if ( inquiryListArray.count>0) {
                        for (int i=0; i< inquiryListArray.count; i++) {
                            [inquiryProductImageArray addObject:[[inquiryListArray objectAtIndex:i] objectForKey:@"ImageUrl"]];
                            [inquiryProductNameArray addObject:[[inquiryListArray objectAtIndex:i] objectForKey:@"ItemName"]];
                            [inquiryWeightArray addObject:[[inquiryListArray objectAtIndex:i] objectForKey:@"Gram"]];
                            [inquiryDateArray addObject:[[inquiryListArray objectAtIndex:i] objectForKey:@"OnHandDate"]];
                            [inquiryPriceArray addObject:[[inquiryListArray objectAtIndex:i] objectForKey:@"SalePrice"]];
                            [inquiryItemCodeArray addObject:[[inquiryListArray objectAtIndex:i] objectForKey:@"ItemCode"]];
                            [inquiryAddressArray addObject:[[inquiryListArray objectAtIndex:i] objectForKey:@"CounName"]];
                            [inquiryAttributeArray addObject:[[inquiryListArray objectAtIndex:i] objectForKey:@"Attributes"]];
                            [inquiryGoldWeightArray addObject:[[inquiryListArray objectAtIndex:i] objectForKey:@"GoldGram"]];
                            [inquiryStyleNameArray addObject:[[inquiryListArray objectAtIndex:i] objectForKey:@"StyleName"] ];
                            [inquiryTypeNameArray addObject:[[inquiryListArray objectAtIndex:i] objectForKey:@"TypeName"] ];
                          
                        }
                    }
                    
                   
                   
                    [_inquiryListTableView reloadData];
                    
                   
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                   [Global loginViewController:self noteStr:requestResultStr];
                     //   [Global loginViewController:self];
                    }

                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
        
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (_refreshHeaderView == nil) {
        
        EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - _inquiryListTableView.bounds.size.height, self.view.frame.size.width, _inquiryListTableView.bounds.size.height)];
        view.delegate = self;
        [_inquiryListTableView addSubview:view];
        _refreshHeaderView = view;
        
        
    }
    
    //  update the last update date
    [_refreshHeaderView refreshLastUpdatedDate];
}
#pragma mark -
#pragma mark Data Source Loading / Reloading Methods

- (void)reloadTableViewDataSource{
    [inquiryProductImageArray removeAllObjects];
    [inquiryProductNameArray removeAllObjects];
    [inquiryWeightArray removeAllObjects];
    [inquiryDateArray removeAllObjects];
    [inquiryPriceArray removeAllObjects];
    [inquiryItemCodeArray removeAllObjects];
    [inquiryAddressArray removeAllObjects];
    [inquiryAttributeArray removeAllObjects];
    [inquiryGoldWeightArray removeAllObjects];
    [inquiryStyleNameArray removeAllObjects];
    [inquiryTypeNameArray removeAllObjects];
    
    _productPageIndex=0;
    [self loadProductNextPage:_productPageIndex];
    //  should be calling your tableviews data source model to reload
    //  put here just for demo
    _reloading = YES;
    
}

- (void)doneLoadingTableViewData{
    
    //  model should call this when its done loading
    _reloading = NO;
    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:_inquiryListTableView];
    
}


#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
     if (scrollView==_inquiryListTableView)
     {
    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
     }
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (scrollView==_inquiryListTableView)
    {
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
    }
}


#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
    
    [self reloadTableViewDataSource];
    [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0];
    
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
    
    return _reloading; // should return if data source model is reloading
    
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
    
    return [NSDate date]; // should return date data source was last changed
    
}


-(IBAction)addToShopCartClick:(id)sender
{
    isLoad=NO;
    if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        [params setObject:itemCodeStr forKey:@"ItemCode"];
        [params setObject:@"A" forKey:@"EditType"];
        
        //  NSLog(@"addToShopCartParams===%@",params);
        [HttpRequest requestData:@"Guide/EditShopCart" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    // NSLog(@"addToShopCart===%@",severDataDict.dataDict);
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
//                    if ([self.delegate respondsToSelector:@selector(stockInquiryShopCartClick)]) {
//                        
//                        [self.delegate stockInquiryShopCartClick];
//                    }
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                      [Global loginViewController:self noteStr:requestResultStr];
                     //   [Global loginViewController:self];
                    }

                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //    NSMutableArray *codeArrays = [NSMutableArray arrayWithArray:[shopCartData objectForKey:@"PRODUCTCODE"]];
    //    return [codeArrays count];
    if (tableView ==_inquiryListTableView) {
        return inquiryProductImageArray.count;
    }
    
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView==_inquiryListTableView)
        return 120.0f;
   
    else
        return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==_inquiryListTableView) {
        static NSString * identifier = @"Cell";
        StockInquiryTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        
        if (cell==nil) {
            NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"StockInquiryTableViewCell" owner:self options:nil];
            cell = [array objectAtIndex:0];
           // cell.delegate=self;
        }
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (inquiryAttributeArray.count>0) {
            if ([inquiryAttributeArray objectAtIndex:indexPath.row]!=[NSNull null]) {
                cell.attributesLabel.text=[NSString stringWithFormat:@"%@",[inquiryAttributeArray objectAtIndex:indexPath.row]];
                
            }
          //  [self resizeLabel: cell.attributesLabel];
            [Global resizeForLable:cell.attributesLabel WithText:cell.attributesLabel.text parentView:self.view];
            CGRect frame= cell.weightTagLabel.frame;
            if ([[inquiryAttributeArray objectAtIndex:indexPath.row] length]>0) {
                
                frame.origin.x=cell.attributesLabel.frame.origin.x+cell.attributesLabel.frame.size.width+12;
            }
            else
            {
                frame.origin.x=cell.attributesLabel.frame.origin.x+cell.attributesLabel.frame.size.width;
            }
            cell.weightTagLabel.frame=frame;
            
            CGRect frame1= cell.weightLabel.frame;
            frame1.origin.x=cell.weightTagLabel.frame.origin.x+cell.weightTagLabel.frame.size.width+1;
            cell.weightLabel.frame=frame1;
            
        }

      
        if (inquiryWeightArray.count>0) {
            cell.weightLabel.text=[NSString stringWithFormat:@"%0.02fg",[[inquiryWeightArray objectAtIndex:indexPath.row] floatValue]];
           //  [self resizeLabel:cell.weightLabel];
            [Global resizeForLable:cell.weightLabel WithText:cell.weightLabel.text parentView:self.view];
            CGRect frame1=cell.goldWeightTagLabel.frame;
            frame1.origin.x=cell.weightLabel.frame.origin.x+cell.weightLabel.frame.size.width+15;
            cell.goldWeightTagLabel.frame=frame1;
            
            CGRect frame2=cell.goldWeightLabel.frame;
            frame2.origin.x=cell.goldWeightTagLabel.frame.origin.x+cell.goldWeightTagLabel.frame.size.width+3;
            cell.goldWeightLabel.frame=frame2;
            

        }
        
        if (inquiryGoldWeightArray.count>0) {
            cell.goldWeightLabel.text=[NSString stringWithFormat:@"%0.02fg",[[inquiryGoldWeightArray objectAtIndex:indexPath.row] floatValue]];
           // [self resizeLabel:cell.goldWeightLabel];
            [Global resizeForLable:cell.goldWeightLabel WithText:cell.goldWeightLabel.text parentView:self.view];
            CGRect frame1=cell.priceTagLabel.frame;
            frame1.origin.x=cell.goldWeightLabel.frame.origin.x+cell.goldWeightLabel.frame.size.width+15;
            cell.priceTagLabel.frame=frame1;
            
            CGRect frame2=cell.priceLabel.frame;
            frame2.origin.x=cell.priceTagLabel.frame.origin.x+cell.priceTagLabel.frame.size.width+3;
            cell.priceLabel.frame=frame2;
            
            
        }

        if (inquiryAddressArray.count>0) {
            cell.addressLabel.text=[inquiryAddressArray objectAtIndex:indexPath.row];
           // [self resizeLabel:cell.addressLabel];
             [Global resizeForLable:cell.addressLabel WithText:cell.addressLabel.text parentView:self.view];
            CGRect frame1=cell.dateTagLabel.frame;
            frame1.origin.x=cell.addressLabel.frame.origin.x+cell.addressLabel.frame.size.width+15;
            cell.dateTagLabel.frame=frame1;
            
            CGRect frame2=cell.dateLabel.frame;
            frame2.origin.x=cell.dateTagLabel.frame.origin.x+cell.dateTagLabel.frame.size.width+3;
            cell.dateLabel.frame=frame2;
            

        }
        if(inquiryDateArray.count>0)
        {
            cell.dateLabel.text=[inquiryDateArray objectAtIndex:indexPath.row];
        }
            
        if (inquiryProductNameArray.count>0&&inquiryItemCodeArray.count>0) {
            NSString *nameStr=[inquiryProductNameArray objectAtIndex:indexPath.row];
            NSString *numStr=[inquiryItemCodeArray objectAtIndex:indexPath.row];
            cell.productNameLabel.text=[NSString stringWithFormat:@"%@ %@",numStr,nameStr];
        }
        if (inquiryPriceArray.count>0) {
            if ([inquiryPriceArray objectAtIndex:indexPath.row]!=[NSNull null]) {
                float AvgPrice=[[inquiryPriceArray objectAtIndex:indexPath.row] floatValue];
                NSString *AvgPriceStr=[Global floatConvertPercentile:AvgPrice];
          //  NSString *marketStr=[NSString stringWithFormat:@"¥%0.02f",[[inquiryPriceArray objectAtIndex:indexPath.row] floatValue]];
                cell.priceLabel.text=[NSString stringWithFormat:@"¥%@",AvgPriceStr];//[shopCartMarketArray objectAtIndex:indexPath.row];
            }
        }
        
        if (inquiryProductImageArray.count>0) {
            NSString *urlStr=[inquiryProductImageArray objectAtIndex:indexPath.row];
            if (urlStr.length>0) {
                [cell.productImageView setImageWithURL:[NSURL URLWithString:urlStr]];
            }
            
            else
            {
                 [cell.productImageView setImage:[UIImage imageNamed:@"zhanweitu_.png"]];
            }
            
        }
        
        if (indexPath.row == [inquiryProductImageArray count] - 1)
        {
            if (_productPageIndex<=_productTotalPage) {
                //    NSLog(@"###LOADING???");
                [self loadProductNextPage:_productPageIndex];
            }
            
        }
        return cell;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    if (tableView==_inquiryListTableView) {
        isLoad=NO;
        inquiryTableTag=indexPath.row;
        itemCodeStr=[inquiryItemCodeArray objectAtIndex:indexPath.row];
    if (inquiryProductImageArray.count>0) {
        [ _productImage setImageWithURL:[NSURL URLWithString:[inquiryProductImageArray objectAtIndex:indexPath.row] ]];
    }
    if (inquiryTypeNameArray.count>0) {
        _styleNameLabel.text=[inquiryTypeNameArray objectAtIndex:indexPath.row];
    }
    if (inquiryStyleNameArray.count>0) {
        _seriesNameLabel.text=[inquiryStyleNameArray objectAtIndex:indexPath.row];
    }
    if (inquiryDateArray.count>0) {
        _dateLabel.text=[inquiryDateArray objectAtIndex:indexPath.row];
    }
    if (inquiryPriceArray.count>0) {
        _priceLabel.text=[NSString stringWithFormat:@"¥%@",[inquiryPriceArray objectAtIndex:indexPath.row] ];
    }
    if(inquiryAddressArray.count>0)
        
    {
        _addressLabel.text=[inquiryAddressArray objectAtIndex:indexPath.row];
    }
    
    if (inquiryWeightArray.count>0) {
        _weightLabel.text=[NSString stringWithFormat:@"%0.02fg",[[inquiryWeightArray objectAtIndex:indexPath.row] floatValue]];
    }
    if (inquiryProductNameArray.count>0) {
        _productNameLabel.text=[NSString stringWithFormat:@"%@ %@",[inquiryItemCodeArray objectAtIndex:indexPath.row],[inquiryProductNameArray objectAtIndex:indexPath.row]];
    }

    for (UIView *v in _rightViewContainer.subviews) {
        if (v.tag==9) {
            
            [v removeFromSuperview];
        }
        
    }
    [_rightScrollView setContentOffset:CGPointMake(0, 0)];
    [self requestItemDetail:[inquiryItemCodeArray objectAtIndex:indexPath.row]];
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame=_productDetailViewContainer.frame;
        
        frame.origin.y=0;
        [_productDetailViewContainer setFrame:frame];
        
    } completion:^(BOOL finished){
        
    }];
  }
    
}

-(void)requestItemDetail:(NSString *)codeStr
{
    _styleNameLabel.text=@"";
    _seriesNameLabel.text=@"";
    _priceLabel.text=@"";
    _addressLabel.text=@"";
    _certIdLabel.text=@"";
    _securitycodeLabel.text=@"";
    _adverbNameLabel.text=@"";
    _neatNessNameLabel.text=@"";
    _shapeLabel.text=@"";
    _adverbQtyNameLabel.text=@"";
    _mainQtyLabel.text=@"";
    _gramLabel.text=@"";
    _itemCodeLabel.text=@"";
    _seriesCodeLabel.text=@"";
    _typeNameLabel.text=@"";
    _designerLabel.text=@"";
    _cadDateLabel.text=@"";
    _itypeNameLabel.text=@"";
    _productNameLabel.text=@"";
    if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        [params setObject:codeStr forKey:@"ItemCode"];
        [HttpRequest requestData:@"Product/ItemDetail" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                     
                    [Global hideProgressHUDInternal:self.view];
                    NSMutableArray *productImgArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataItemImgs"]];
                    if (productImgArray.count>0) {
                        _productImageBtn.hidden=NO;
                        _upImageButton.hidden=YES;
                        [_productImage setImageWithURL:[NSURL URLWithString:[[productImgArray objectAtIndex:0] objectForKey:@"ImageUrl"]]];
                        imageURL=[[productImgArray objectAtIndex:0] objectForKey:@"ImageUrl"];
                        
                        CGRect frame=_rightViewContainer.frame;
                        frame.origin.y=_productImageBtn.frame.origin.y+_productImageBtn.frame.size.height+10;
                        _rightViewContainer.frame=frame;
                        //   NSLog(@"frame.origin.y====%f",frame.origin.y);
                    }
                    else
                    {
                        _productImageBtn.hidden=YES;
                        _upImageButton.hidden=NO;
                        CGRect frame=_rightViewContainer.frame;
                        frame.origin.y=_upImageButton.frame.origin.y+_upImageButton.frame.size.height+10;
                        _rightViewContainer.frame=frame;
                        //   NSLog(@"frame.origin.y111====%f",frame.origin.y);
                        
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ItemName"]!=[NSNull null]) {
                        _productNameLabel.text=[NSString stringWithFormat:@"%@ %@",[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ItemCode"],[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ItemName"]];
                    }

                    if ([[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ItypeName"]!=[NSNull null]) {
                        _styleNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ItypeName"];
                        
                        _styleNameLabel.numberOfLines = 0;
                        _styleNameLabel.lineBreakMode = NSLineBreakByWordWrapping;
                        CGSize maximumLabelSize = CGSizeMake(1000, _styleNameLabel.frame.size.height);
                        
                        CGSize expectSize = [_styleNameLabel sizeThatFits:maximumLabelSize];
                        CGRect frame1 = CGRectMake(_styleNameLabel.frame.origin.x, _styleNameLabel.frame.origin.y,expectSize.width, _styleNameLabel.frame.size.height );
                        _styleNameLabel.frame=frame1;
                        
                        
                        // [self resizeLabel:_styleNameLabel];
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"MainQty"]!=[NSNull null]) {
                        _mainQtyLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"MainQty"];
                        
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"Size"]!=[NSNull null]) {
                        _sizeLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"Size"];
                        
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ISectypeName"]!=[NSNull null]) {
                        _seriesNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"ISectypeName"];
                        
                        _seriesNameLabel.numberOfLines = 0;
                        _seriesNameLabel.lineBreakMode = NSLineBreakByWordWrapping;
                        CGSize maximumLabelSize = CGSizeMake(1000, _seriesNameLabel.frame.size.height);
                        
                        CGSize expectSize = [_seriesNameLabel sizeThatFits:maximumLabelSize];
                        CGRect frame1 = CGRectMake(_seriesNameLabel.frame.origin.x,_seriesNameLabel.frame.origin.y,expectSize.width,_seriesNameLabel.frame.size.height );
                        _seriesNameLabel.frame=frame1;
                        
                        
                        // [self resizeLabel:_seriesNameLabel];
                        CGRect frame0=_seriesNameLabel.frame;
                        frame0.origin.x=_styleNameLabel.frame.origin.x+_styleNameLabel.frame.size.width+3;
                        _seriesNameLabel.frame=frame0;
                    }
                    
                    if ([[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"Price"]!=[NSNull null]) {
                        _priceLabel.text=[NSString stringWithFormat:@"¥%@",[Global floatConvertPercentile:[[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"Price"] floatValue]]];
                        
                        //  [self resizeLabel:_priceLabel];
                        _priceLabel.numberOfLines = 0;
                        _priceLabel.lineBreakMode = NSLineBreakByWordWrapping;
                        CGSize maximumLabelSize = CGSizeMake(1000,_priceLabel.frame.size.height);
                        
                        CGSize expectSize = [_priceLabel sizeThatFits:maximumLabelSize];
                        CGRect frame0 = CGRectMake(_priceLabel.frame.origin.x, _priceLabel.frame.origin.y,expectSize.width, _priceLabel.frame.size.height );
                        _priceLabel.frame=frame0;
                        
                        
                        CGRect frame1=_weightTagLabel.frame;
                        frame1.origin.x=_priceLabel.frame.origin.x+_priceLabel.frame.size.width+10;
                        _weightTagLabel.frame=frame1;
                        
                        CGRect frame2=_weightLabel.frame;
                        frame2.origin.x=_weightTagLabel.frame.origin.x+_weightTagLabel.frame.size.width+3;
                        _weightLabel.frame=frame2;
                    }
                    
                    if ([[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"InDate"]!=[NSNull null]) {
                        _dateLabel.text=[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"InDate"];
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"WhsName"]!=[NSNull null]) {
                        
                        _addressLabel.text=[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"WhsName"];
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"GoldGram"]!=[NSNull null]) {
                        _weightLabel.text=[NSString stringWithFormat:@"%@g",[Global floatConvertPercentile:[[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"GoldGram"] floatValue]]];
                    }
                    
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"CertId"]!=[NSNull null]) {
                        _certIdLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"CertId"];
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"Securitycode"]!=[NSNull null]) {
                        _securitycodeLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"Securitycode"];
                        
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"AdverbName"]!=[NSNull null]) {
                        _adverbNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"AdverbName"];
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"NeatNess"]!=[NSNull null]) {
                        
                        _neatNessNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"NeatNess"];
                    }
                    
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"Shape"]!=[NSNull null]) {
                        _shapeLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"Shape"];
                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"AdverbQty"]!=[NSNull null]) {
                        _adverbQtyNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"AdverbQty"];
                    }
                    
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"GoldGram"]!=[NSNull null]) {
                        _gramLabel.text=[NSString stringWithFormat:@"%@g",[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"GoldGram"]];
                    }
//                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"ItemCode"]!=[NSNull null])
//                    {
//                        _itemCodeLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"ItemCode"];
//                    }
                    if ([[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"ItemCode"]!=[NSNull null])
                    {
                        _itemCodeLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"ArtNo"];
                    }

                    NSString *productTextViewStr=[[severDataDict.dataDict objectForKey:@"DataItem"] objectForKey:@"Introduction"];
                    _productTextView.text=productTextViewStr;
                    CGFloat height=  [Global heightForTextView:_productTextView WithText:_productTextView.text];
                    
                    CGRect frameP=_productTextView.frame;
                    frameP.size.height=height;
                    _productTextView.frame=frameP;
                    
                    NSString *seriesCodeStr=[[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"SeriesCode"];
                  
                    if (seriesCodeStr.length>0) {
                       // _detailNameLabel.text=@"款式详情";
                        _seriesView.hidden=NO;
                    
                        CGRect frameA=_seriesView.frame;
                        frameA.origin.y=_productTextView.frame.origin.y+_productTextView.frame.size.height+5;
                        _seriesView.frame=frameA;
                        
                        CGRect frameB=_detailLabelView.frame;
                        frameB.origin.y=_seriesView.frame.origin.y+_seriesView.frame.size.height+5;
                        _detailLabelView.frame=frameB;
                        
                        _seriesCodeLabel.text=seriesCodeStr;
                        
                        if ([[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"SeriesName"]!=[NSNull null])
                        {
                            _typeNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"SeriesName"];
                        }
                        if ([[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"Designer"]!=[NSNull null])
                        {
                            _designerLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"Designer"];
                        }
                        if ([[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"CadDate"]!=[NSNull null])
                        {
                            
                            _cadDateLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"CadDate"];
                        }
                        if ([[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"ItypeName"]!=[NSNull null])
                        {
                            _itypeNameLabel.text=[[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"ItypeName"];
                        }
                        NSString *seriesViewStr=[[severDataDict.dataDict objectForKey:@"DataHeadInf"] objectForKey:@"Introduction"];
                        _seriesTextView.text=seriesViewStr;
                        CGFloat height=  [Global heightForTextView:_seriesTextView WithText:_seriesTextView.text];
                        
                        CGRect frameP=_seriesTextView.frame;
                        frameP.size.height=height;
                        _seriesTextView.frame=frameP;
                        
                        NSMutableArray *mTagsArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLineLabel"]];
                      //  NSLog(@"mTagsArray===%@",mTagsArray);
                        
                        float height1=0.0;
                        for (int i=0; i<mTagsArray.count; i++) {
                            UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(83, 0, 85, 32)];
                            [label setFont:[UIFont fontWithName:@"fontWithName" size:15]];
                            [label setText:[[mTagsArray objectAtIndex:i] objectForKey:@"LabelName"]];
                       //     [self resizeLabel:label];
                            [Global resizeForLable:label WithText:label.text parentView:self.view];
                            CGRect rect=label.frame;
                            rect.size.height=32;
                            rect.size.width=label.frame.size.width;
                            rect.origin.x=86+(label.frame.size.width+15)*(i%6);
                            rect.origin.y=46*(i/6);
                            label.frame=rect;
                            label.tag=9;
                            [label setTextColor:[UIColor colorWithRed:209.0f/255.0f green:174.0f/255.0f blue:80.0f/255.0f alpha:1.0f] ];
                            [_seriesTagView addSubview:label];
                            
                        }
                        height1=height1+(mTagsArray.count/6+1)*35;
                        CGRect frameT=_seriesTagView.frame;
                        frameT.size.height=height1;
                        _seriesTagView.frame=frameT;
                        
                        CGRect frameS=_seriesView.frame;
                        frameS.size.height=_seriesTagView.frame.size.height+_seriesTagView.frame.origin.y;
                        _seriesView.frame=frameS;
                        
                        //                    }
                        
                    }
                    else
                    {
                        
                        _seriesView.hidden=YES;
                        CGRect frameB=_detailLabelView.frame;
                        frameB.origin.y=_productTextView.frame.origin.y+_productTextView.frame.size.height+5;
                        _detailLabelView.frame=frameB;
                        
                    }
                    
                    //webview
                    NSString *urlStr=[[severDataDict.dataDict objectForKey:@"DataHeadDetail"] objectForKey:@"ItemDespUrl"];
                    
                    CGRect rect = _itemUrlWebView.frame;
                    rect.origin.y=_detailLabelView.frame.origin.y+_detailLabelView.frame.size.height+10;
                    rect.size.height=1;
                    
                    _itemUrlWebView.frame=rect;
                    _itemUrlWebView.scrollView.bounces = NO;
                    [_itemUrlWebView setScalesPageToFit:NO];
                    _itemUrlWebView.scrollView.showsHorizontalScrollIndicator = NO;
                    _itemUrlWebView.scrollView.scrollEnabled = NO;
                    // [_itemUrlWebView scalesPageToFit];
                    // NSLog(@"urlStr===%@",urlStr);
                    
                    if (urlStr.length>0) {
                        
                        // [_itemUrlWebView setPaginationMode:UIWebPaginationModeTopToBottom];
                        NSURL *url = [[NSURL alloc]initWithString:urlStr];
                        [_itemUrlWebView loadRequest:[NSURLRequest requestWithURL:url]];
                        
                        
                    }
                    
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                      [Global loginViewController:self noteStr:requestResultStr];  
                       // [Global loginViewController:self];
                    }

                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
        
    }
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if (webView==_itemUrlWebView) {
        
        [self performSelector:@selector(getWebHeight) withObject:nil afterDelay:0.3];
        
    }
}
-(void)getWebHeight
{
   // [_itemUrlWebView sizeToFit];
    NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", _itemUrlWebView.frame.size.width];
    [_itemUrlWebView stringByEvaluatingJavaScriptFromString:meta];
    
    
    CGSize actualSize = [_itemUrlWebView sizeThatFits:CGSizeZero];
    CGRect newFrame = _itemUrlWebView.frame;
    newFrame.size.height = actualSize.height;
    _itemUrlWebView.frame = newFrame;
    
    //  CGSize newsize=CGSizeMake(620, 356+_itemUrlWebView.frame.size.height);
    //_itemUrlWebView.contentSize=newsize;
    
    
    
    CGFloat height = _itemUrlWebView.scrollView.contentSize.height;
    // NSLog(@"height===%f",height);
    
    
    CGRect rect1 = _rightViewContainer.frame;
    rect1.size.height=_itemUrlWebView.frame.origin.y+height;
    _rightViewContainer.frame=rect1;
    
    [_rightScrollView setScrollEnabled:YES];
    
    [_rightScrollView setContentSize:CGSizeMake(0, _rightViewContainer.frame.origin.y+_rightViewContainer.frame.size.height)];
    //  NSLog(@"frame gggg= %f",htmlHeight);
    [_rightScrollView setShowsVerticalScrollIndicator:NO];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
        if (textField==_searchProductTextField) {
        [inquiryProductImageArray removeAllObjects];
        [inquiryProductNameArray removeAllObjects];
        [inquiryWeightArray removeAllObjects];
        [inquiryDateArray removeAllObjects];
        [inquiryPriceArray removeAllObjects];
        [inquiryItemCodeArray removeAllObjects];
        [inquiryAddressArray removeAllObjects];
        [inquiryAttributeArray removeAllObjects];
        [inquiryGoldWeightArray removeAllObjects];
        [inquiryStyleNameArray removeAllObjects];
        [inquiryTypeNameArray removeAllObjects];

        _productPageIndex=0;
        [self loadProductNextPage:_productPageIndex];
        [_searchProductTextField resignFirstResponder];
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
